//
//  CertificationQualificationViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/21.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "CertificationQualificationViewController.h"
#import "CertificationCollectionViewCell.h"
static NSString * CellIdentifier = @"CertificationCollectionViewCell";


@interface CertificationQualificationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,MWPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property(nonatomic,strong)NSArray * imagesArray;

@property(nonatomic,strong)NSMutableArray * photos;
@property(nonatomic,strong)MWPhotoBrowser * browser;
@end

@implementation CertificationQualificationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _photos = [NSMutableArray array];
    [self qualification];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _imagesArray = [NSArray array];
    [self initViewLayout];
}

- (void)initViewLayout {
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 505), NO);
    
    _nameTextField.userInteractionEnabled = NO;

    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.userInteractionEnabled=YES;
    [self.collectionView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    _browser = [[MWPhotoBrowser alloc] init];
    
    // Set options
    _browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    _browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    _browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    _browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    _browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    _browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    _browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    
    // Optionally set the current visible photo before displaying
    // Manipulate
    [_browser showNextPhotoAnimated:YES];
    [_browser showPreviousPhotoAnimated:YES];
}


- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 认证资质
- (void)qualification {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_mallExpertId forKey:@"mallExpertId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_muceLicence withHttpType:@"POST" withTag:JY_JY_muceLicence];
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.imagesArray.count;
    
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(158*VerticalRatio(), 110*VerticalRatio());
    
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0,0, 0, 0);
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CertificationCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *imageStr = [NSString stringWithFormat:@"%@",self.imagesArray[indexPath.row]];
    if (imageStr && imageStr.length>0&&![self.imagesArray[indexPath.row] isEqual:[NSNull null]]) {
        [cell.imagView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,imageStr]]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _browser.titleStr = @"资质证件";
    _browser.delegate = self;
    [_browser setCurrentPhotoIndex:0];
    // Present
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_browser];
    [self presentViewController:nc animated:YES completion:nil];

}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}

- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index{
    NSString * str = [NSString stringWithFormat:@"%ld/%ld",index+1,_photos.count];
    return str;
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==JY_JY_muceLicence) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary *dic = database[@"result"];
            
            if (![dic[@"muceRealName"] isEqual:[NSNull null]]) {
                 _nameTextField.text = [NSString stringWithFormat:@"%@",dic[@"muceRealName"]];
             }
            
            if (![dic[@"muceLicence"] isEqual:[NSNull null]]) {
                NSString * images = [NSString stringWithFormat:@"%@",dic[@"muceLicence"]];
                images = [images substringToIndex:images.length-1];
                _imagesArray = [images componentsSeparatedByString:@","];
                for (NSString *url in _imagesArray) {
                    NSString * URL = [NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,url];
                    MWPhoto * photo = [[MWPhoto alloc]initWithURL:[NSURL URLWithString:URL]];
                    [_photos addObject:photo];
                }
                [_collectionView reloadData];
            }
           
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}


@end
